<?php 
namespace backend\modules\business\methods\cashier;
use Yii;
use yii\data\Pagination;
use common\models\BusMember;
use common\models\Status;
use common\models\BusConfig;
use common\models\BusRepertory;
use backend\modules\business\methods\base\Basemethod;
use common\models\BusEmployee;
use common\models\BusMemberDebt;
/**
 * 财务收银(所有弹出框的操作)
 * @author      miaoyu
 * @date        2016-2-25
 * @version     2.0
 * @copyright   Copyright 2016 meelier.com
 */
class Cashier extends Basemethod
{
	/**
	 * 收银首页
	 * @date: 2016-3-1 
	 * @author: futao
	 */
	public function index()
	{
		unset($_SESSION['member_id']);
		$where = $this->where;
		$where[] = 'debt_state = 0';
		$whereStr = implode(' and ', $where);
		$count = BusMemberDebt::find()
		->where($whereStr)
		->count();
		return $this->render('index',['count'=>$count]);
	}
	/**
	 * 获取会员列表
	 * @date: 2016-2-27 
	 * @author: futao
	 */
	public function getUserlist()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$this->pageNavLimit=5;
		$where = $this->where;
		if($this->getParam('name')!=''){
			if(is_numeric($this->getParam('name'))){
				$where[]='member_mobile like :name';
				$bind[':name']='%'.$this->getParam('name').'%';
			}else{
				$where[]='member_name like :name';
				$bind[':name']='%'.$this->getParam('name').'%';
			}
		}
		$where[] = 'del_flag = 1';
		$whereStr = implode(' and ',$where);
		$query = BusMember::find()
		->select([
			'member_id',
			'member_name',
			'member_mobile',
			'member_grade'
		])
		->where($whereStr,$bind);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->orderBy("member_id desc")
		->asArray()
		->all();
		$cardList = BusConfig::find()
		->where("config_type = 1 and shop_id = {$this->userInfo->shop_id}")
		->asArray()
		->all();
		return $this->render('userlist',['list'=>$list,'page'=>$page,'name'=>$this->getParam('name'),'card_list'=>$cardList]);
	}
	/**
	 * 更新用户数据
	 * @date: 2016-2-27 
	 * @author: futao
	 */
	public function updateUserinfo()
	{
		$name = $this->postParam('name','');
		$mobile = $this->postParam('mobile','');
		$grade = $this->postParam('grade','');
		$id = intval($this->postParam('id',0));
		if($id<1){
			$this->errorjs('会员ID错误!');
		}
		if($name==''){
			$this->errorjs('姓名不能为空!');
		}
		$where = $this->where;
		$where[] = 'member_id = '.$id;
		$where[] = 'del_flag = '.Status::NORMAL;
		$whereStr = implode(' and ',$where);
		$info = BusMember::find()->where($whereStr)->one();
		if(!$info){
			$this->errorjs('error!');
		}
		$memberModel = new BusMember();
		$data['member_mobile'] = $mobile;
		$data['member_name'] = $name;
		$data['member_grade'] = $grade;
		if($memberModel->updateMemberinfo($id,$data)===false){
			$this->errorjs($memberModel->errorMessage);
		} else {
			$this->errorjs('修改成功',1);
		}

	}
	/**
	 * 添加会员
	 * @date: 2016-2-29 
	 * @author: futao
	 */
	public function addUser()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		if(!Yii::$app->request->isPost){
			return $this->render('add');
		}
		$name = $this->postParam('name','');
		$mobile = $this->postParam('mobile',''); 
		if($name == ""){
			$this->errorjs("姓名不能为空!");
		}
		$memberModel = new BusMember();
		$data['member_name'] = $name;
		$data['shop_id'] = $this->userInfo->shop_id;
		$data['boss_id'] = $this->userInfo->boss_uid;
		$data['member_mobile'] = $mobile;
		if(!$memberModel->addMember($data)){
			$this->errorjs($memberModel->errorMessage);
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 充值卡列表
	 * @date: 2016-3-1 
	 * @author: futao
	 * type 1  充值卡
	 * type 2  疗程卡
 	 * type 3  合作项目
 	 * type 4  服务项目
	 * type 5  房间
 	 * type 6  库存
	 */
	public function balanceCard()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$where = $this->where;
		$where[] = 'config_type = 1';
		$whereStr = implode(' and ',$where);
		$list = BusConfig::find()
		->where($whereStr)
		->asArray()
		->all();
		return $this->render('balancecard',['list'=>$list]);
	}
	/**
	 * 更新储值卡名称
	 * @date: 2016-3-1 
	 * @author: futao
	 */
	public function updateBalancecard() 
	{
		$id = intval($this->postParam('config_id',0));
		$title = $this->postParam('title',0);
		$where = $this->where;
		$where[] = 'config_id = :id';
		$bind[':id'] = $id;
		$whereStr = implode(" and ",$where);
		$info = BusConfig::find()->where($whereStr,$bind)->one();
		if(!$info){
			$this->errorjs('参数错误!');
		}
		$info->config_key = $title;
		if($info->save(false)===false){
			$this->errorjs('修改失败!');
		}
		return true;
	}
	/**
	 * 添加储值卡
	 * @date: 2016-3-1 
	 * @author: futao
	 */
	public function addBalancecard()
	{
		$key = $this->postParam('title','');
		if($key==''){
			$this->errorjs("请填写名称!");
		}
		$configModel = new BusConfig();
		if(!$configModel->addCard(1, $key, '0', '', $this->userInfo->shop_id)){
			$this->errorjs("新增失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 疗程卡列表
	 * @date: 2016-3-2 
	 * @author: futao
	 */
	public function healthCardlist()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$this->pageNavLimit = 5;
		$name = $this->getParam('title','');
		$where = $this->where;
		$where[] = 'config_key like :name';
		$bind[':name'] = '%'.$name.'%';
		$where[] = 'config_type = 2';
		$whereStr = implode(' and ',$where);
		$query = BusConfig::find()
		->where($whereStr,$bind);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->orderBy("config_id desc")
		->asArray()
		->all();
		return $this->render('healthcard',['list'=>$list,'page'=>$page]);
	}
	/**
	 * 修改疗程卡
	 * @date: 2016-3-3 
	 * @author: futao
	 */
	public function updateHealthcard()
	{
		$key = $this->postParam('key','');
		$val = $this->postParam('val','');
		$val2 = $this->postParam('val2','');
		$id = intval($this->postParam('id',0));
		if($id<1){
			$this->errorjs('ID错误!');
		}
		$configModel = new BusConfig();
		if(!$configModel->updateCard($key, $val, $val2, $id, $this->userInfo->shop_id)){
			$this->errorjs("修改失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 添加疗程卡
	 * @date: 2016-3-3 
	 * @author: futao
	 */
	public function addHealthcard()
	{
		$key = $this->postParam('key','');
		$val = $this->postParam('val','');
		$val2 = $this->postParam('val2','');
		$configModel = new BusConfig();
		if(!$configModel->addCard('2', $key, $val, $val2, $this->userInfo->shop_id)){
			$this->errorjs("新增失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 家居产品列表
	 * @date: 2016-3-3 
	 * @author: futao
	 */
	public function productList()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$this->pageNavLimit = 5;
		$name = $this->getParam('title','');
		$where = $this->where;
		$where[] = 'is_sell = 1';
		$where[] = 'repertory_name like :name';
		$bind[':name'] = '%'.$name.'%';
		$whereStr = implode(' and ',$where);
		$query = BusRepertory::find()
		->where($whereStr,$bind);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->orderBy('repertory_id desc')
		->asArray()
		->all();
		return $this->render('product',['list'=>$list,'page'=>$page]);
	}
	/**
	 * 添加产品
	 * @date: 2016-3-3 
	 * @author: futao
	 */
	public function addProduct()
	{
		$count = $this->postParam('count');
		$name = $this->postParam('name');
		$sellPrice = $this->postParam('sell_price');
		$repertory = new BusRepertory();
		$data['shopId'] = $this->userInfo->shop_id;
		$data['bossId'] = $this->userInfo->boss_uid;
		$data['uid'] = $this->userInfo->uid;
		$data['repertory_name'] = $name;
		$data['repertory_count'] = $count;
		$data['goods_sell_price'] = $sellPrice;
		if(!$repertory->addRepertory($data)){
			$this->errorjs($repertory->getErrormessage());
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 编辑产品
	 * @date: 2016-3-4 
	 * @author: futao
	 */
	public function updateProduct()
	{
		$name = $this->postParam('name');
		$sellPrice = $this->postParam('sell_price');
		$id = intval($this->postParam('id'));
		$shopId = $this->userInfo->shop_id;
		$repertory = new BusRepertory();
		$data['repertory_name'] = $name;
		$data['goods_sell_price'] = $sellPrice;
		if($repertory->editRepertory($id,$shopId,$data)===false){
			$this->errorjs($repertory->getErrormessage());
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 合作项目列表
	 * @date: 2016-3-4 
	 * @author: futao
	 */
	public function cooperationProjectsList()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$this->pageNavLimit = 5;
		$name = $this->getParam('title','');
		$where = $this->where;
		$where[] = 'config_key like :name';
		$bind[':name'] = '%'.$name.'%';
		$where[] = 'config_type = 3';
		$whereStr = implode(' and ',$where);
		$query = BusConfig::find()
		->where($whereStr,$bind);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->orderBy('config_id desc')
		->asArray()
		->all();
		return $this->render('cooperationproject',['list'=>$list,'page'=>$page]);
	}
	/**
	 * 添加合作项目
	 * @date: 2016-3-4 
	 * @author: futao
	 */
	public function addCooperationProjects()
	{
		$key = $this->postParam('key','');
		$val = $this->postParam('val','');
		$val2 = $this->postParam('val2','');
		$configModel = new BusConfig();
		if(!$configModel->addCard('3', $key, $val, $val2, $this->userInfo->shop_id)){
			$this->errorjs("新增失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 修改合作项目
	 * @date: 2016-3-4 
	 * @author: futao
	 */
	public function updateCooperationProjects()
	{
		$key = $this->postParam('key','');
		$val = $this->postParam('val','');
		$val2 = $this->postParam('val2','');
		$id = intval($this->postParam('id',0));
		if($id<1){
			$this->errorjs('ID错误!');
		}
		$configModel = new BusConfig();
		if(!$configModel->updateCard($key, $val, $val2, $id, $this->userInfo->shop_id)){
			$this->errorjs("新增失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 服务项目列表
	 * @date: 2016-3-4
	 * @author: futao
	 */
	public function serviceProjectsList()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$this->pageNavLimit = 5;
		$name = $this->getParam('title','');
		$where = $this->where;
		$where[] = 'config_key like :name';
		$bind[':name'] = '%'.$name.'%';
		$where[] = 'config_type = 4';
		$whereStr = implode(' and ',$where);
		$query = BusConfig::find()
		->where($whereStr,$bind);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->orderBy('config_id desc')
		->asArray()
		->all();
		return $this->render('serviceproject',['list'=>$list,'page'=>$page]);
	}
	/**
	 * 添加服务项目
	 * @date: 2016-3-4
	 * @author: futao
	 */
	public function addServiceProjects()
	{
		$key = $this->postParam('key','');
		$val = $this->postParam('val','');
		$val2 = $this->postParam('val2','');
		$configModel = new BusConfig();
		if(!$configModel->addCard('4', $key, $val, $val2, $this->userInfo->shop_id)){
			$this->errorjs("新增失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 修改服务项目
	 * @date: 2016-3-4
	 * @author: futao
	 */
	public function updateServiceProjects()
	{
		$key = $this->postParam('key','');
		$val = $this->postParam('val','');
		$val2 = $this->postParam('val2','');
		$id = intval($this->postParam('id',0));
		if($id<1){
			$this->errorjs('ID错误!');
		}
		$configModel = new BusConfig();
		if(!$configModel->updateCard($key, $val, $val2, $id, $this->userInfo->shop_id)){
			$this->errorjs("新增失败!");
		}else{
			$this->errorjs('ok',1);
		}
	}
	/**
	 * 员工列表
	 * @date: 2016-3-7 
	 * @author: futao
	 */
	public function staffList()
	{
		$this->setlayout('/alert');
		$this->disableDebugtool();
		$this->pageNavLimit = 5;
		$where = $this->where;
		$where[] = 'del_flag = 1';
		$whereStr = implode(' and ',$where);
		$query = BusEmployee::find()
		->where($whereStr);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->orderBy("employee_id desc")
		->asArray()
		->all();
		return $this->render('staff',['list'=>$list,'page'=>$page]);	
	}	
}